package com.zhonghui.scm.service.impl;

import com.zhonghui.common.utils.StringUtils;
import com.zhonghui.scm.domain.ScmSaleDelivery;
import com.zhonghui.scm.domain.ScmSaleDeliveryItem;
import com.zhonghui.scm.domain.ScmSaleOrder;
import com.zhonghui.scm.domain.ScmSaleOrderItem;
import com.zhonghui.scm.mapper.ScmSaleDeliveryMapper;
import com.zhonghui.scm.service.IScmSaleDeliveryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 销售发货Service业务层处理
 *
 * @author zhonghui
 * @date 2022-05-26
 */
@Service
public class ScmSaleDeliveryServiceImpl implements IScmSaleDeliveryService
{
    @Autowired
    private ScmSaleDeliveryMapper scmSaleDeliveryMapper;

    /**
     * 查询销售计划
     *
     * @param id 销售计划主键
     * @return 销售计划
     */
    @Override
    public ScmSaleDelivery getById(Long id)
    {

        return scmSaleDeliveryMapper.getById(id);
    }

    /**
     * 查询销售计划列表
     *
     * @param scmSaleDelivery 销售计划
     * @return 销售计划
     */
    @Override
    public List<ScmSaleDelivery> getList(ScmSaleDelivery scmSaleDelivery)
    {
        return scmSaleDeliveryMapper.getList(scmSaleDelivery);
    }

    /**
     * 新增销售计划
     *
     * @param scmSaleDelivery 销售计划
     * @return 结果
     */
    @Override
    public int add(ScmSaleDelivery scmSaleDelivery)
    {
        scmSaleDelivery.setDeliveryNo(generateNumber("XSJH"));
        int row = scmSaleDeliveryMapper.add(scmSaleDelivery);
        insertScmSaleDeliveryItem(scmSaleDelivery);
        return row;
    }

    /**
     * 修改销售计划
     *
     * @param scmSaleDelivery 销售计划
     * @return 结果
     */
    @Transactional
    @Override
    public int edit(ScmSaleDelivery scmSaleDelivery)
    {

        scmSaleDeliveryMapper.delItem(scmSaleDelivery.getId());
        insertScmSaleDeliveryItem(scmSaleDelivery);
        return scmSaleDeliveryMapper.edit(scmSaleDelivery);
    }

    /**
     * 批量删除销售计划
     *
     * @param ids 需要删除的销售计划主键
     * @return 结果
     */
    @Override
    public int del(Long[] ids)
    {
        for (Long id : ids) {
            scmSaleDeliveryMapper.delItem(id);
        }

        return scmSaleDeliveryMapper.del(ids);
    }

    public String generateNumber(String number){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return number+format.format(date);
    }

    public void insertScmSaleDeliveryItem(ScmSaleDelivery scmSaleDelivery)
    {
        List<ScmSaleDeliveryItem> scmSaleDeliveryItemList = scmSaleDelivery.getScmSaleDeliveryItemList();
        if (StringUtils.isNotNull(scmSaleDeliveryItemList))
        {
            List<ScmSaleDeliveryItem> list = new ArrayList<ScmSaleDeliveryItem>();
            for (ScmSaleDeliveryItem scmSaleDeliveryItem : scmSaleDeliveryItemList)
            {
                scmSaleDeliveryItem.setDeliveryId(scmSaleDelivery.getId());
                list.add(scmSaleDeliveryItem);
            }
            if (list.size() > 0)
            {
                scmSaleDeliveryMapper.batchScmSaleOrderItem(list);
            }
        }
    }

}
